Minimizing Context Switch With JetBrains HTTP Client Tool
February 4, 2024 • 4 minutes • 739 words
If you're reading this it means you decided to stick around even after you've seen my cringe-worthy header image I created, CONGRATS! You successfully passed the test! Now, I'll show you the way to Mordor! jk, just the way to a faster development workflow.When I write my code, I want to keep my focus as much as possible (especially with my self-diagnosed-by-tiktok ADHD) and shifting between dev tools is in and out of itself a tiny context switch. I used to work closely with Postman/Postwoman for API calls while developing (still do in some cases) but when I found out I can test my API without leaving my IDE, I got excited and gave it a go. Fear not PyCharm users, this awesome tool also exists for you!
To be completely honest, JetBrains IDEs were never my first pick as an IDE but ever since I started using it again in my new job, I’m discovering more and more helpful features that I would have needed to search and install in other IDEs. So kudos IntelliJ, you’ve made it in my top favorite IDEs.
Requirements
A running server with http API endpoints (I’ll be using IntelliJ by JetBrains)
Example Code
You can follow along in a basic LOTR API server I created with 2 endpoints (Node.js), download it right here
GET /character (No auth)
POST /character (with HMAC authentication)
Let’s Start the journey, fellowship
Initiate HTTP Client
Open your project in intelij and create your first request
Alter the default basic request to your required endpoint:
GET http://localhost:8080/character
Accept: application/json
Click on the play icon
and we have a response!
How about adding an Auth T̶o̶k̶e̶n̶ Tolkien?
So, out of the box, HTTP hands out a bunch of great snippets for all authorization methods as follows:
here’s a simple auth header example:
GET http://localhost:8080/character
Accept: application/json
Authorization: Bearer 123
Let’s take the example of an HMAC authentication which is a hash-based message authentication code. Since it requires hashing action before the actual request being sent, Here’s an example of a pre-script for hashing, in the supported language.
< {%
request.variables.set("signature", crypto.hmac.sha256()
.withTextSecret('lotr-server-example') // hmac token
.digest().toHex())
%}
Now, we probably want to reuse this token or change it from one env to the other, this is where the use of Environments feature comes in:
It’s basically a .json
file with the env vars in the following format:
{
"dev": {
"hmac": "lotr-server-example"
}
}
so now we can replace the token in the pre-script with the env var:
(...).withTextSecret(request.environment.get('hmac')) // hmac token by env
select the specific environment from the top toolbar and hit Run
again.
Post it
The full post request with body parameters and our authentication:
### POST characters
< {%
request.variables.set("signature", crypto.hmac.sha256()
.withTextSecret(request.environment.get('hmac'))
.digest().toHex())
%}
POST http://localhost:8080/character
Content-Type: application/json
X-TOKEN: {{signature}}
{
"name": "Rubi",
"birthYear": "1988"
}
YOU SHELL NOT PASS
If you encounter the above bellow, it means the environment is either not selected or the variable does not exist.
Runtime error: Parameter specified as non-null is null:
method com.intellij.httpClient.http.request.run.preScript
“Even the smallest p̶e̶r̶s̶o̶n̶ tool can change the course of the future”
While using this tool for a while I realized that with a simple click (or keyboard shortcut for those who are cool as me) I get to check the code quicker, which made my workflow so much easier. I can define several calls and eventually make these calls in to actual tests!
Gimli more!
It’s a good start for you to try it out and I also recommend to read more of the longer version in JetBrains documentation for more elaborate API workflow.
What’s that? you already have the request in Postman or cURL? No sweat! In the Convert cURL to HTTP Request dialog, paste the cURL request that you want to convert:
The file that has become My Precious
After a while of using this, I thought this can be used for documentation or knowledge base for expected API calls requests. So, I initiated it with our team’s tech lead to commit it to our repository on Github.
There are numerous possibilities to share this information but one of my favorite advantages of using it over other tools is having it all in one place which make the maintenance much easier.
Adding this directory can save time in word-to-mouth hacks of example calls.
Hope you had a blast of reading this post just like I had writing it, until next time!